package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsGrouponRules;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsGrouponRulesService;
import com.qf.service.DtsStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/21 20:58
 */
@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {
    @Autowired
    DtsGrouponRulesMapper grouponRulesMapper;
    @Autowired
    DtsStorageService storageService;
    @Autowired
    DtsStorageMapper storageMapper;

    @Override
    public Map<String, Object> selectlistuserByPage(Integer page, Integer limit, String sort, String order, Integer goodsId) {

        IPage<DtsGrouponRules> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();

        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        if (goodsId!=null){
            wrapper.like("goods_id",goodsId);
        }

        grouponRulesMapper.selectPage(iPage,wrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());

        return map;
    }

    @Override
    public void insert(DtsGrouponRules grouponRules) {
        grouponRulesMapper.insert(grouponRules);
    }

    @Override
    public void update(DtsGrouponRules grouponRules) {
        grouponRulesMapper.updateById(grouponRules);
    }

    @Override
    public void delete(DtsGrouponRules grouponRules) {
        LambdaQueryWrapper<DtsGrouponRules> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGrouponRules::getGoodsId,grouponRules.getGoodsId());
        grouponRules.setDeleted(true);
        grouponRulesMapper.update(grouponRules,wrapper);

    }
}
